package com.zzyl.nursing.service.impl;
import java.util.List;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.zzyl.nursing.vo.HandleAlertDataRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
    /**
     * 处理设备报警数据
     *
     * @param requestList 报警数据处理请求列表
     * @return 结果
     */
    @Override
    public int handleAlertDataList(List<HandleAlertDataRequest> requestList) {
        int successCount = 0;

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        for (HandleAlertDataRequest request : requestList) {
            AlertData alertData = new AlertData();
            alertData.setId(request.getId());
            alertData.setProcessingResult(request.getProcessingResult());
            // 新增：设置处理人id和名称
            alertData.setProcessorId(request.getProcessorId());
            alertData.setProcessorName(request.getProcessorName());

            // 将字符串时间转换为 LocalDateTime
            if (request.getProcessingTime() != null && !request.getProcessingTime().isEmpty()) {
                LocalDateTime processingTime = LocalDateTime.parse(request.getProcessingTime(), formatter);
                alertData.setProcessingTime(processingTime);
            }

            // 更新状态为已处理
            alertData.setStatus(1);

            if (updateById(alertData)) {
                successCount++;
            }
        }
        return successCount;
    }

}
